package com.ericsson.eritask.dao.impl;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.ericsson.eritask.dao.UserDAO;
import com.ericsson.eritask.domain.User;

@Repository
public class UserDaoImpl implements UserDAO {

	
	
	@PersistenceContext
	EntityManager entityManager;
	
	@Transactional
	@Override
	public void updateUserImage(String imageId, String signum) {

		System.out.println("imageid in updateuserimage"+imageId+signum);
		System.out.println("before entityManager.find(User.class, signum):::"+entityManager.find(User.class, signum));
		User u =entityManager.find(User.class, signum);
		
		u.setImageId(imageId);
		entityManager.merge(u);
		
	}

	@Transactional
	@Override
	public String getImageId(String signum) {
		
		System.out.println("signum in getImageId()..."+signum);
		User u =entityManager.find(User.class, signum);
		String imageId = u.getImageId();
		System.out.println("imageId in getImageId()..."+imageId);
		return imageId;
	}

	
	@Transactional
	@Override
	public User findByUserName(String userName) {

		System.out.println("findByUserName():::"+userName);
		Query q= entityManager.createQuery("select u from User u where u.userName= :userName",User.class);
		q.setParameter("userName", userName);
		System.out.println("select u from User u where u.userName= :userName"+q.getResultList());
		List<User> users = q.getResultList();
		System.out.println("List<User>::::"+users);
		return users.get(0);
	}
	
	

}
